import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class 城市群数量1 {
    /*
    https://www.nowcoder.com/practice/71cde4dee669475f94d8d38832374ada?tpId=196&tqId=40411&ru=/exam/oj
    用dfs解决
     */

    public static void main(String[] args) {

    }
    public static int n;
    public static boolean[] vis;

    public int citys (ArrayList<ArrayList<Integer>> t) {
        // write code here
        n = t.size();
        vis = new boolean[n];
        int ret = 0;
        for(int i = 0;i < n;i++){
            if(!vis[i]){
                ret++;
                dfs(t,i);
            }
        }
        return ret;
    }

    private void dfs(ArrayList<ArrayList<Integer>> t,int pos){
        vis[pos] = true;
        for(int i = 0;i < n;i++){
            if(t.get(i).get(pos) == 1 && !vis[i]){
                vis[i] = true;
                dfs(t,i);
            }
            if(t.get(pos).get(i) == 1 && !vis[i]){
                vis[i] = true;
                dfs(t,i);
            }
        }
    }
}
